<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use DateTimeInterface;

class Order extends Model
{
    protected $table = 'order';
    public $data = [
        'where' => [],
        'select' => '*',
        'page' => 1,
        'num' => 6,
        'order' => 'id desc'
    ];


    /**
     * 列表(分页)
     * @return mixed
     */
    public function getList()
    {
        $query = self::selectRaw($this->data['select']);
        $query = $this->whereFilter($this->data['where'], $query);
        return $query->orderByRaw($this->data['order'])->paginate($this->data['num'], ['*'], 'page', $this->data['page'])->toArray();
    }

    /**
     * 虚拟人问答-列表(全部)
     * @return mixed
     */
    public function getListAll()
    {
        $query = self::selectRaw($this->data['select']);
        $query = $this->whereFilter($this->data['where'], $query);
        $query = $query->orderByRaw($this->data['order'])->get();
        return empty($query) ? [] : $query->toArray();
    }

    /**
     * 虚拟人问答-单条
     * @return mixed
     */
    public function getInfo()
    {
        $query = self::selectRaw($this->data['select']);
        $query = $this->whereFilter($this->data['where'], $query);
        $connect =  $query->first();
        return empty($connect) ? array():$connect->toArray();
    }

    /**
     * 虚拟人问答-编辑
     * @param $data
     * @return mixed
     */
    public function editInfo($data)
    {
        $query = self::from('order');
        $query = $this->whereFilter($this->data['where'], $query);
        return $query->update($data);
    }

    /**
     * 虚拟人问答-添加
     * @param $data
     * @return mixed
     */
    public static function getAddId($data){
        $data['created_at'] = date('Y-m-d H:i:s');
        $data['updated_at'] = date('Y-m-d H:i:s');
        return self::insertGetId($data);
    }

    /**
     * 虚拟人问答-添加
     * @param $data
     * @return mixed
     */
    public static function add($data){
        if (isset($data[0])){
            foreach ($data as $key => $val){
                $data[$key]['created_at'] = date('Y-m-d H:i:s');
                $data[$key]['updated_at'] = date('Y-m-d H:i:s');
            }
        }else{
            $data['created_at'] = date('Y-m-d H:i:s');
            $data['updated_at'] = date('Y-m-d H:i:s');
        }
        return self::insertGetId($data);
    }

    /**
     * 虚拟人问答-条数
     * @return mixed
     */
    public function getCount()
    {
        $query = self::from('order');
        $query = $this->whereFilter($this->data['where'], $query);
        return $query->count();
    }

    /**
     * 虚拟人问答-单一字段数组
     * @param $field
     * @return array
     */
    public function getFieldArr($field)
    {
        $query = self::from('order');
        $query = $this->whereFilter($this->data['where'], $query);
        $connect =  $query->pluck($field);
        return empty($connect) ? array() : $connect->toArray();
    }

    /**
     * 获取配置的键值对
     * @param $key
     * @param $val
     * @return mixed
     */
    public function getMap($key,$val)
    {
        $query = self::from('order');
        $query = $this->whereFilter($this->data['where'],$query);
        return $query->pluck($val, $key)->toArray();
    }


    /**
     * 条件过滤
     * @param $where
     * @param $query
     * @return mixed
     */
    public function whereFilter($where, $query)
    {
        if (isset($where['or']) && !empty($where['or'])){
            $query->where($where['or']);
            unset($where['or']);
        }
        if (isset($where['in']) && !empty($where['in'])) {
            $query = $query->whereIn($where['in'][0], $where['in'][1]);
            unset($where['in']);
        }
        return $query->where($where);
    }

    /**
     * Prepare a date for array / JSON serialization.
     *
     * @param \DateTimeInterface $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
}
